package personal.vivian.util;

import com.google.common.cache.*;
import org.assertj.core.util.Lists;
import personal.vivian.dto.Eks;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CacheUtil {
    public static void main(String[] args) {
        LoadingCache<Integer, Eks> eksLoadingCache= CacheBuilder.newBuilder().concurrencyLevel(8).expireAfterWrite(8, TimeUnit.SECONDS)
                .initialCapacity(10).maximumSize(100).recordStats().removalListener(new RemovalListener<Object, Object>() {
                    @Override
                    public void onRemoval(RemovalNotification<Object, Object> removalNotification) {
                        System.out.println(removalNotification.getKey() + " was removed, cause is " + removalNotification.getCause());
                    }
                }).build(new CacheLoader<Integer, Eks>() {
                    @Override
                    public Eks load(Integer integer) throws Exception {
                        System.out.println("load:"+integer);
                        Eks eks = new Eks();
                        eks.setDirection(integer);
                        List<Integer> dots = Lists.newArrayList();
                        eks.setDotxs(dots);
                        dots.add(new Integer(1));
                        dots.add(new Integer(2));
                        return eks;
                    }
                });
        for(int i = 0;i<20;i++){
            Eks eks = null;
            try {
                eks = eksLoadingCache.get(1);
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            System.out.println(eks);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("cache stat");
        System.out.println(eksLoadingCache.stats().toString());
    }
}
